<?php
require_once("oj-header.php");
require_once("./include/db_info.inc.php");
?>

<?php
echo "<title>Search Result</title>";
echo "<center><table width=90%>";

$keyword = $_GET["keyword"];
$condition = $_GET["condition"];

$add = "";
if($condition == 'Title')
	$add = " `title` like'%".$keyword."%' ";
else
	$add = " `source` like'%".$keyword."%' ";

echo "<font color='red'><h3>Search Result</h3></font>";

$sub_arr=Array();
// submit
if (isset($_SESSION['user_id'])){
$sql="SELECT `problem_id` FROM `solution` WHERE `user_id`='".$_SESSION['user_id']."'".
	" group by `problem_id`";
$result=@mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result))
	$sub_arr[$row[0]]=true;
}

$acc_arr=Array();
// ac
if (isset($_SESSION['user_id'])){
$sql="SELECT `problem_id` FROM `solution` WHERE `user_id`='".$_SESSION['user_id']."'".
	" AND `result`=4".
	" group by `problem_id`";
$result=@mysql_query($sql) or die(mysql_error());
while ($row=mysql_fetch_array($result))
	$acc_arr[$row[0]]=true;
}

if (!isset($_SESSION['administrator'])){
	$sql="SELECT `problem_id`,`title`,`submit`,`accepted`,`source` FROM `problem` ".
	"WHERE ".$add." and `defunct`='N' AND `problem_id` NOT IN(
		SELECT `problem_id` FROM `contest_problem` WHERE `contest_id` IN (
			SELECT `contest_id` FROM `contest` WHERE `end_time`>NOW()
		)
	)";
}
else{
	$sql="SELECT `problem_id`,`title`,`submit`,`accepted`,`source` FROM `problem` where ".$add;
}

$sql=$sql." ORDER BY `problem_id`";
$result = mysql_query($sql) or die(mysql_error());
?>

<!-- problem search -->
<tr><td colspan='4'><form action='problemsearch.php' method='get'>
Search:
<input type='text' size='20' name='keyword' value='<?=$keyword?>' />
<select name='condition' value='0'>
	<!--option value='0'>Title</option>
	<option value='1'>Source</option>-->
<?php
	for($i=0; $i<2; $i++) {
		echo "<option vaule='".$i."'";
		if($i == 0 && $condition == "Title") echo " selected ";
		if($i == 1 && $condition == "Source") echo " selected ";
		echo ">";
		if($i == 0) echo "Title";
		else echo "Source";
		echo "</option>";
	}
?>
</select>
<input  type='submit' value='Go' style='width:50px'/>
</form>
</td>
</tr>

<!-- End of problem search -->

<?php
echo "<tr align=center class='toprow'><td width=5><td width=10%>Problem ID<td width=75%>Title<td width=15%>Ratio(AC/Submit)</tr>";
$cnt=0;
while ($row=mysql_fetch_object($result)){
	if ($cnt) echo "<tr class='oddrow'>";
	else echo "<tr class='evenrow'>";
	echo "<td>";
	if ($sub_arr[$row->problem_id]){
		if ($acc_arr[$row->problem_id]) echo "<span class=yes>Y</span>";
		else echo "<span class=no>N</span>";
	}
	
	echo "<td align=center>".$row->problem_id;
	echo "<td align=left><a href='problem.php?id=".$row->problem_id."'>".$row->title."</a>";
	echo "&nbsp;-&nbsp;"."<font color='gray'>".$row->source."</font>";
	//echo "<td align=left><a href='category.php?id=1'>Dynamic Program"."</a>";
	echo "<td align=center><a href='status.php?problem_id=".$row->problem_id."&jresult=4'>"
		.$row->accepted."</a>/<a href='status.php?problem_id=".$row->problem_id."'>".$row->submit."</a>";
	echo "</tr>";
	$cnt=1-$cnt;
}

mysql_free_result($result);
echo "</center></table>";
?>

<?php require_once("oj-footer.php"); ?>
